package com.moco.launcher.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;

import com.moco.launcher.R;
import com.moco.launcher.link.LinkManager;
import com.moco.launcher.play.AndroidMusicPlayer;
import com.moco.launcher.view.ShutDownDialog;
import com.oazon.moco.link.core.LinkEventController;
import com.oazon.moco.link.core.LinkSDK;
import com.oazon.moco.link.core.source.LinkEventSource;

public class ShutDownActivity extends BaseActivity implements OnClickListener {
	private static final String TAG = "ShutDownActivity";
	
	private LinearLayout mCancel, mConfirm;
	public static final String ACTION_REQUEST_SHUTDOWN = "android.intent.action.ACTION_REQUEST_SHUTDOWN";
	public static final String CONFIRM = "android.intent.action.EXTRA_KEY_CONFIRM";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.shutdown_activity);
		
		mCancel = (LinearLayout)findViewById(R.id.cancel);
		mConfirm = (LinearLayout)findViewById(R.id.confirm);

		mCancel.setOnClickListener(this);
		mConfirm.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		if (v.getId() == mCancel.getId()) {
			cancel();
		}
		
		if (v.getId() == mConfirm.getId()) {
			confirm();
		}
	}

	private void confirm() {
		AndroidMusicPlayer.getInstance().stop();
		ShutDownDialog dialog = new ShutDownDialog(this, "正在关机");
		dialog.show();
		//LinkManager.getInstance().setShutdone();
		LinkSDK.getInstance().isShutdone(LinkEventController.uiSource);
		shutDown();
		
		
	}
	private void shutDown() {
		try {
			Intent shutdown = new Intent(ACTION_REQUEST_SHUTDOWN);
			shutdown.putExtra(CONFIRM, false);
			shutdown.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
					| Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(shutdown);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void cancel() {
		finish();
	}
}
